'''
Created on Sep 3, 2011

@author: david
'''

from app.G6.actions import G6Action

class Logon(G6Action):
    """
    Default Action 
    spawns index.gen
    """
    def execute(self):
        import logging
        from app.G6.agents import AccountAgent 
        from app.G6.agents import AccountException
        import simplejson as json
        
        aa = AccountAgent()
        
        req = self.request
        rslt = "200::OK"
        nxtPage = "/G6/blogger.gen"
        msg = None
        
        # check credentials...
        try:
            uid = req.getParameter("uid")
            pwd = req.getParameter("pwd")
            acct = aa.fetch(uid, pwd)
            
            if not acct:
                raise AccountException("Access Denied: Invalid Credentials for %d" % uid)
            
            self.sess_check(True)

            req.session.setAttribute('acct',acct)
        
            logging.info("++ Login: %s" % uid)
        
        except Exception, ex:
                # invalid!!!!
                rslt = "401::%s" % str(ex)
                nxtPage = "NO_CHANGE"
                logging.exception("!! Access Exception:")
                msg = "Access Denied (Invalid Credentials)"
                
        return self.JSONOutput(json.dumps({ "results": rslt, "next": nxtPage, "msg": msg }))
    